Linux环境下的Python翻译工具源码

您所在的位置:网站首页 linux 翻译软件 Linux环境下的Python翻译工具源码

Linux环境下的Python翻译工具源码

#Linux环境下的Python翻译工具源码| 来源: 网络整理| 查看: 265

Linux环境下的Python翻译工具源码

玩蛇网Python教程源码示例,本文源码是用于Linux环境下的Python翻译工具源码详解。 学习计算机编程语言一定会涉及到英文和学习应用,但通常英语词典在Linux环境下都不如Win环境下的好用。所以为了方便自己使用,作者花了些时间用python写了个翻译工具。

程序员的编程工作大都在Linux环境下和vim编辑器,基于以上两点出发,开始写代码喽!

Python Linux环境翻译工具

使用Python语言编写完成的翻译工具,需要用到urllib和BeautifulSoup 类库。 urllib:主要负责网络通讯方面; BeautifulSoup:负责HTML的解析。 这也是Python语言生态圈的强大之处,有了这两个得力的"助手"写个这样的python翻译工具,毫不费力!

简单的来说下用于Linux环境下的Python翻译工具的原理:获取根据用户输入的单词——提交给百度词典——读取百度词典的翻译——返回的数据并解析——最后将处理过的数据显示出来。如果有能力也可以把Python代码适当的改写一下,把百度翻译换成有道翻译也是可以的。

以下是该Python翻译工具的具体代码

import urllib import codecs from BeautifulSoup import BeautifulSoup from sys import argv import re,time class Translate: def Start(self): self._get_html_sourse() self._get_content("enc") self._remove_tag() self.print_result() def _get_html_sourse(self): word=argv[1] if len(argv)>1 else '' url="http://dict.baidu.com/s?wd=%s&tn=dict" % word self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore") def _get_content(self,div_id): soup=BeautifulSoup("".join(self.htmlsourse)) self.data=str(soup.find("div",{"id":div_id})) def _remove_tag(self): soup=BeautifulSoup(self.data) self.outtext=''.join([element for element in soup.recursiveChildGenerator() if isinstance(element,unicode)]) def print_result(self): for item in range(1,10): self.outtext=self.outtext.replace(str(item),"\n%s" % str(item)) self.outtext=self.outtext.replace(" ","\n") print self.outtext if __name__=="__main__": Translate().Start()

Linux环境下的Python翻译工具源码,第二部分:

import urllib import codecs from BeautifulSoup import BeautifulSoup from sys import argv import re,time class Translate: def Start(self): self._get_html_sourse() self._get_content("enc") self._remove_tag() self.print_result() def _get_html_sourse(self): word=argv[1] if len(argv)>1 else '' url="http://dict.baidu.com/s?wd=%s&tn=dict" % word self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore") def _get_content(self,div_id): soup=BeautifulSoup("".join(self.htmlsourse)) self.data=str(soup.find("div",{"id":div_id})) def _remove_tag(self): soup=BeautifulSoup(self.data) self.outtext=''.join([element for element in soup.recursi 2000 veChildGenerator() if isinstance(element,unicode)]) def print_result(self): for item in range(1,10): self.outtext=self.outtext.replace(str(item),"\n%s" % str(item)) self.outtext=self.outtext.replace(" ","\n") print self.outtext if __name__=="__main__": Translate().Start()

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/linux/l2552.html

相关文章 Recommend • linux系统 修改include出现问题 • 求大牛看下python源码中的__init__()作用是什么 • 分析python源码找到新闻阅读器无法运行的原因, • Python源码里类似@xxx这样符号是什么意思 • linux + python2 + zsh, unicode编码显示异常 • linux下多个python版本切换如何设置 • Linux控制台终端进行自动处理Web认证方法 • Linux 系统中nginx是否适合用在工控的嵌入式设备上 • 贴个python源码求分析,微信第三方接口无法为客户 • Discuz scrapy模拟登录Python源码如何写 • 文中Python源码中%10的作用是什么 • Python源码中三引号作用是什么

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群 修订日期:2016年12月27日 - 17时27分55秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python源码实例 > Linux编程源码 > 正文内容 我要分享到: 上一篇:Python实时监控Linux cpu使用率实现方法 下一篇:没有了 必知PYTHON教程 Must Know PYTHON Tutorials • python 解释器 • python idle • python dir函数 • python 数据类型 • python type函数 • python 字符串 • python 整型数字 • python 列表 • python 元组 • python 字典 • python 集合 • python 变量 • python print • python 函数 • python 类定义 • python import • python help • python open • python 异常处理 • python 注释 • python continue • python pass • python return • python global • python assert • python if语句 • python break • python for循环 • python while循环 • python else/elif • lambda匿名函数 必知PYTHON模块 Must Know PYTHON Modules • os 模块 • sys 模块 • re 正则表达式 • time 日期时间 • pickle 持久化 • random 随机 • csv 模块 • logging 日志 • socket网络通信 • json模块 • urlparse 解析URL • urllib 模块 • urllib2 模块 • robotparser 解析 • Cookie 模块 • smtplib 邮件 • Base64 编码 • xmlrpclib客户端 • string 文本 • Queue 线程安全 • math数学计算 • linecache缓存 • threading多线程 • sqlite3数据库 • gzip压缩解压


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3